//Source file: D:\\aaa\\com\\ce\\pms\\renovation\\service\\impl\\RenovateSupervisionServiceImpl.java

package com.ce.pms.renovation.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.renovation.dao.IInspectionRequisitionDao;
import com.ce.pms.renovation.dao.IReformDao;
import com.ce.pms.renovation.dao.IRenovateLicenceDao;
import com.ce.pms.renovation.dao.IRenovateViolationDao;
import com.ce.pms.renovation.model.AbstractViolationDO;
import com.ce.pms.renovation.model.InspectionRequisitionDO;
import com.ce.pms.renovation.model.ReformDO;
import com.ce.pms.renovation.model.RenovateLicenceDO;
import com.ce.pms.renovation.model.RenovateViolationDO;
import com.ce.pms.renovation.service.IRenovateManageService;
import com.ce.pms.renovation.service.IRenovateSupervisionService;
import com.ce.pms.renovation.vo.AbstractViolationVO;
import com.ce.pms.renovation.vo.ReformVO;
import com.ce.pms.renovation.vo.RenovateViolationConditionVO;
import com.ce.pms.renovation.vo.RenovateViolationVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 名称: com.ce.pms.renovation.service.impl.RenovateSupervisionServiceImpl
 * 装修管理服务实现类
 *
 * @author 许心炜 <br>
 *         Created on： 2008-12-19 <br>
 * @author <br>
 *         update on： <br>
 *         why: <br>
 */
public class RenovateSupervisionServiceImpl extends PmsBaseServiceImpl
		implements IRenovateSupervisionService {

	private IRenovateViolationDao renovateViolationDao;

	private IReformDao reformDao;

	private IRenovateLicenceDao renovateLicenceDao;

	@SuppressWarnings("unused")
	private IRenovateManageService renovateManageService;

	@SuppressWarnings("unused")
	private IHouseService houseService;

	private IInspectionRequisitionDao inspectionRequisitionDao;

	/**
	 * @roseuid 49473AFC009C
	 */
	public RenovateSupervisionServiceImpl() {

	}

	/**
	 * 新增监管期违规
	 *
	 * @author 许心炜
	 * @param renvoateViolationVO -
	 *            监管期违规VO
	 * @return RenovateViolationVO
	 * @roseuid 49473AFC00CB
	 */
	public RenovateViolationVO addRenovateViolation(
			RenovateViolationVO renovateViolationVO) {
		// if(renvoateViolationVO.getRenovateLicenceId())

		if (!checkRenovateViolationField(renovateViolationVO)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		// 先验证是否有验收申请了
		// isViolation(renovateViolationVO.getRenovateLicenceId());

		if (BlankUtil.isBlank(renovateViolationVO.getRenovateLicenceId())) {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_nolicence_id_not_add",
					BusinessException.ERROR);
		} else {
			if (!isAllowAddRenovateViolation(renovateViolationVO
					.getRenovateLicenceId())) {
				throw new BusinessException(
						"renovate:renovate_renovateviolation_has_inspectionrequisitions_not_add",
						BusinessException.ERROR);
			}
		}

		RenovateViolationDO renovateViolationDO = new RenovateViolationDO();
		renovateViolationDO = assembleRenovateViolationV2D(renovateViolationVO,
				renovateViolationDO);
		// 需验证是否保存成功？
		renovateViolationDO.setState("001");// 新建时，为未整改状态
		renovateViolationDO.setSysInfo(new SysInfo(renovateViolationVO
				.getCreator(), renovateViolationDao.getDate()));
		renovateViolationDO = (RenovateViolationDO) renovateViolationDao
				.saveObject(renovateViolationDO);
		renovateViolationVO = assembleRenovateViolationD2V(renovateViolationDO,
				renovateViolationVO);
		return renovateViolationVO;
	}

	/**
	 * 修改监管期违规
	 *
	 * @author 许心炜
	 * @param renvoateViolationVO -
	 *            监管期违规VO
	 * @return RenovateViolationVO
	 * @roseuid 49473AFC00DB
	 */
	public RenovateViolationVO editRenovateViolation(
			RenovateViolationVO renvoateViolationVO) {
		if (!checkRenovateViolationField(renvoateViolationVO)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		if (!isAllowEditRenovateViolation(renvoateViolationVO.getKeyword())) {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_has_reforms_not_edit",
					BusinessException.ERROR);
		}

		RenovateViolationDO renovateViolationDO = (RenovateViolationDO) renovateViolationDao
				.findById(renvoateViolationVO.getKeyword());
		renovateViolationDO = assembleRenovateViolationV2D(renvoateViolationVO,
				renovateViolationDO);
		// 需验证是否保存成功？
		renovateViolationDO.setSysInfo(new SysInfo(renvoateViolationVO
				.getModifier(), this.renovateViolationDao.getDate()));
		renovateViolationDO = (RenovateViolationDO) renovateViolationDao
				.updateObject(renovateViolationDO);
		renvoateViolationVO = assembleRenovateViolationD2V(renovateViolationDO,
				renvoateViolationVO);
		return renvoateViolationVO;
	}

	/**
	 * 删除监管期违规
	 *
	 * @author 许心炜
	 * @param renovateViolationId -
	 *            监管期违规ID
	 * @roseuid 49473AFC00FA
	 */
	public Boolean deleteRenovateViolation(Long renovateViolationId) {
		if (!isAllowDeleteRenovateViolation(renovateViolationId)) {
			throw new BusinessException("common:delete_error_exception",
					BusinessException.FAILURE);
		}

		renovateViolationDao.deleteObjectById(renovateViolationId);
		return true;

	}

	/**
	 * 获得监管期违规记录VO，包括整改记录
	 *
	 * @author 许心炜
	 * @return RenovateViolationVO
	 * @roseuid 49473AFC010A
	 */
	public RenovateViolationVO getRenovateViolation(Long renovateViolationId) {
		if (BlankUtil.isBlank(renovateViolationId)) {
			throw new BusinessException("违规id不能为空");
		}

		RenovateViolationDO renovateViolationDO = renovateViolationDao
				.getRenovateViolation(renovateViolationId);
		RenovateViolationVO renovateViolationVO = new RenovateViolationVO();
		renovateViolationVO = assembleRenovateViolationD2V(renovateViolationDO,
				renovateViolationVO);
		return renovateViolationVO;
	}

	/**
	 * 获得整改记录列表
	 *
	 * @author 许心炜
	 * @param renovateViolationId -
	 *            获得某监管期违规的整改记录列表
	 * @return List
	 * @roseuid 49473AFC0128
	 */
	public List<ReformVO> getReforms(Long renovateViolationId) {
		if (BlankUtil.isBlank(renovateViolationId)) {
			throw new BusinessException("违规id不能为空");
		}

		List<ReformVO> list = new ArrayList<ReformVO>();
		List<ReformDO> list2 = reformDao.getReforms(renovateViolationId);

		for (ReformDO reformDO : list2) {
			ReformVO reformVO = new ReformVO();
			assembleReformD2V(reformDO, reformVO);
			list.add(reformVO);
		}

		return list;
	}

	/**
	 * 新增整改记录
	 *
	 * @author 许心炜
	 * @param reformVO -
	 *            整改记录VO
	 * @return ReformVO
	 * @roseuid 49473AFC0138
	 */
	public ReformVO addReform(ReformVO reformVO) {
		if (!checkReformField(reformVO)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		if (!isAllowAddReform(reformVO.getRenovateViolationId())) {
			throw new BusinessException(
					"renovate:renovate_reform_has_passed_not_add",
					BusinessException.ERROR);
		}

		ReformDO tempDO = new ReformDO();
		ReformDO reformDO = assembleReformV2D(reformVO, tempDO);
		// 需验证是否保存成功？
		reformDO.setSysInfo(new SysInfo(reformVO.getCreator(), reformDao
				.getDate()));
		reformDO = reformDao.saveObject(reformDO);

		reformVO = assembleReformD2V(reformDO, reformVO);
		notifyAddReform(reformVO.getKeyword());

		return reformVO;
	}

	/**
	 * 删除整改记录：如果有未通过记录要把违规记录变为未通过，如果没有任何记录就需要改为未整改，有通过的才改为通过状态.
	 * 如果已经做了验收申请了，通过的记录也不能删除了
	 *
	 * @author 许心炜
	 * @param reformId -
	 *            整改记录ID
	 * @roseuid 49473AFC0148
	 */
	public Boolean deleteReform(Long reformId) {
		if (BlankUtil.isBlank(reformId)) {
			throw new BusinessException("请选择一条记录");
		}

		if (!isAllowDeleteReform(reformId)) {
			// throw new BusinessException("请先删除通过状态的记录");
			throw new BusinessException(
					"renovate:renovate_reform_first_del_passed",
					BusinessException.ERROR);
		}

		notifyDeleteReform(reformId);

		reformDao.deleteObjectById(reformId);
		return true;

	}

	/**
	 * 功能描述: 得到指定许可证的所有违规记录 <br>
	 *
	 * @param licenceId
	 * @return Create author:许心炜 <br>
	 *         Create on:2009-1-7 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public List<AbstractViolationVO> getViolationsByLicenceId(Long licenceId) {
		if (BlankUtil.isBlank(licenceId)) {
			throw new BusinessException("许可证ID不能为空");
		}

		List<AbstractViolationVO> list = new ArrayList<AbstractViolationVO>();
		List<AbstractViolationDO> list2 = renovateViolationDao
				.getViolationsByLicenceId(licenceId);

		for (AbstractViolationDO abstractViolationDO : list2) {
			AbstractViolationVO abstractViolationVO = new AbstractViolationVO();
			BeanUtil.convertVoAsDo(abstractViolationDO, abstractViolationVO);
			list.add(abstractViolationVO);
		}

		return list;
	}

	/**
	 * 监管期违规记录列表高级查询
	 *
	 * @author 许心炜
	 * @param queryCondition -
	 *            组合查询条件
	 * @return Page
	 * @roseuid 49473AFC0196
	 */
	public Page queryRenovateViolations(
			RenovateViolationConditionVO queryCondition) {
		Page page = renovateViolationDao
				.queryRenovateViolations(queryCondition);

		List<RenovateViolationDO> list = page.getResult();

		List<RenovateViolationConditionVO> tempList = new ArrayList<RenovateViolationConditionVO>();

		for (RenovateViolationDO renovateViolationDO : list) {
			RenovateViolationConditionVO renovateViolationConditionVO = new RenovateViolationConditionVO();
			assembleRenovateViolationConditionD2V(renovateViolationDO,
					renovateViolationConditionVO);

			tempList.add(renovateViolationConditionVO);
		}

		page.setResult(tempList);
		return page;
	}

	/**
	 * 功能描述: 是否处于监管期 <br>
	 *
	 * @param licenceId--许可证id
	 * @return Create author:许心炜 <br>
	 *         Create on:2009-1-7 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public Boolean isViolation(Long licenceId) {
		if (BlankUtil.isBlank(licenceId)) {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_has_inspectionrequisitions_not_add",
					BusinessException.ERROR);
			// return false;
		}

		RenovateLicenceDO renovateLicenceDO = renovateLicenceDao
				.findById(licenceId);

		if("001".equals(renovateLicenceDO.getState())) {
			if ("002".equals(renovateLicenceDO.getRenovateRequisition()
					.getProcessState())) {
				return true;
			} else {
				throw new BusinessException(
						"renovate:renovate_renovateviolation_has_inspectionrequisitions_not_add",
						BusinessException.ERROR);
				// return false;
			}
		}else {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_nolicence_id_not_add",
					BusinessException.ERROR);
		}

	}

	/**
	 * 功能描述: 获得通过的整改记录 <br>
	 * 用于是否还能新增整改记录判断所用
	 *
	 * @param renovateViolationId
	 * @return Create author:许心炜 <br>
	 *         Create on:2009-1-13 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public ReformVO getPassReform(Long renovateViolationId) {
		ReformDO reformDO = reformDao.getPassReform(renovateViolationId);
		ReformVO reformVO = new ReformVO();
		if (reformDO != null) {
			reformVO = assembleReformD2V(reformDO, reformVO);
			return reformVO;
		} else {
			return null;
		}
	}
	/**
	 * 功能描述: 通过ID获得整改记录 <br>
	 * 用于是否还能新增整改记录判断所用
	 *
	 * @param reformID
	 * @return Create author:许心炜 <br>
	 *         Create on:2009-1-13 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public ReformVO getReform(Long reformID) {
		ReformDO reformDO = reformDao.findById(reformID);
		ReformVO reformVO = new ReformVO();
		if (reformDO != null) {
			reformVO = assembleReformD2V(reformDO, reformVO);
			return reformVO;
		} else {
			return null;
		}
	}

	/**
	 * 是否允许新增监管期违规:许可证已发并且装修申请的装修进程处于监管状态，此许可证ID还没有验收申请记录
	 *
	 * @author 许心炜
	 * @param licenceId -
	 *            许可证Id
	 * @return boolean
	 * @roseuid 493F1ABB01D4
	 */
	private boolean isAllowAddRenovateViolation(Long licenceId) {
		// 根据ID查到许可证
		RenovateLicenceDO renovateLicenceDO = this.renovateLicenceDao
				.findById(licenceId);
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.getInspectionRequisitionByLicenceId(licenceId);

		// 发证状态 001-已发，002-未发
		if (renovateLicenceDO.getRenovateRequisition().getProcessState()
				.equals("002")) {// 002-监管
			if (renovateLicenceDO.getState().equals("001")) {
				if (inspectionRequisitionDO == null) {
					return true;
				} else {
					throw new BusinessException(
							"renovate:renovate_renovateviolation_has_inspectionrequisitions_not_add",
							BusinessException.ERROR);
					// return false;
				}
			} else {
				throw new BusinessException(
						"renovate:renovate_renovateviolation_nolicence_id_not_add",
						BusinessException.ERROR);
				// return false;
			}
		} else {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_not_in_renovate_not_add",
					BusinessException.ERROR);
			// return false;
		}

	}

	/**
	 * 是否允许修改监管期违规:对于已经整改验收的装修申请不得修改
	 *
	 * @author 许心炜
	 * @param renovateSupervisionId -
	 *            监管期违规Id
	 * @return boolean
	 * @roseuid 493F21B70222
	 */
	private boolean isAllowEditRenovateViolation(Long renovateSupervisionId) {
		if (BlankUtil.isBlank(renovateSupervisionId)) {
			throw new BusinessException("监管期违规Id为空");
			// return false;
		}

		RenovateViolationDO renovateViolationDO = (RenovateViolationDO) renovateViolationDao
				.findById(renovateSupervisionId);
		if (renovateViolationDO != null
				&& renovateViolationDO.getReforms().size() <= 0) {
			return true;
		} else {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_has_reforms_not_edit",
					BusinessException.ERROR);
			// return false;
		}

	}

	/**
	 * 是否允许删除监管期违规：对于已经存在整改验收记录的违规登记不得删除
	 *
	 * @author 许心炜
	 * @param renovateSupervisionId -
	 *            监管期违规Id
	 * @return boolean
	 * @roseuid 493F22BB009C
	 */
	private boolean isAllowDeleteRenovateViolation(Long renovateSupervisionId) {
		RenovateViolationDO renovateViolationDO = (RenovateViolationDO) renovateViolationDao
				.findById(renovateSupervisionId);
		if (renovateViolationDO != null
				&& renovateViolationDO.getReforms().size() <= 0) {
			return true;
		} else {
			throw new BusinessException(
					"renovate:renovate_reform_has_reforms_not_del",
					BusinessException.ERROR);
			// return false;
		}
	}

	/**
	 * 是否允许新增整改记录：
	 *
	 * @author 许心炜
	 * @param renovateViolationId -
	 *            监管期违规ID
	 * @roseuid 493F3081006D
	 */
	private boolean isAllowAddReform(Long renovateViolationId) {
		// 如果有一条违规记录已经为整改通过的状态，就不允许新增操作了。
		RenovateViolationDO renovateViolationDO = (RenovateViolationDO) renovateViolationDao
				.findById(renovateViolationId);

		// 整改状态，001-未整改， 002-不通过， 003-通过
		if (renovateViolationDO != null
				&& "003".equals(renovateViolationDO.getState())) {
			throw new BusinessException(
					"renovate:renovate_reform_has_passed_not_add",
					BusinessException.ERROR);
			// return false;
		} else {
			return true;
		}
	}

	/**
	 * 是否允许删除整改记录
	 *
	 * @author 许心炜
	 * @param reformId -
	 *            整改记录的ID
	 * @roseuid 493F31030251
	 */
	private boolean isAllowDeleteReform(Long reformId) {
		// 如果这条违规记录已经为整改通过的状态，就不允许新增操作了。
		// 验收结果 001-通过，002-不通过;整改状态 001-未整改， 002-不通过， 003-通过
		// 如果违规记录已经是通过的状态，说明有一条整改通过的记录了，
		// 那么如果删除的这条记录是未通过状态的，就必须先删除通过状态的才可以
		ReformDO tempDO = reformDao.findById(reformId);
		Long licenceId = tempDO.getRenovateViolationDO().getRenovateLicence()
				.getKeyword();
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.getInspectionRequisitionByLicenceId(licenceId);
		if (inspectionRequisitionDO == null) {
			if ("003".equals(tempDO.getRenovateViolationDO().getState())) {
				if ("002".equals(tempDO.getResult())) {
					throw new BusinessException(
							"renovate:renovate_reform_first_del_passed",
							BusinessException.ERROR);
					// return false;
				} else {
					return true;
				}
			} else {// 整改状态为不通过状态就可以删除
				return true;
			}
		} else {// 已经做了验收申请了，被引用了
			throw new BusinessException("common:delete_error_exception",
					BusinessException.ERROR);
			// return false;
		}
	}

	/**
	 * @author 许心炜
	 * @param renovateViolationVO
	 * @param renovateViolationDO
	 * @return com.ce.pms.renovation.model.RenovateViolationDO
	 * @roseuid 493F1ED6003E
	 */
	private RenovateViolationDO assembleRenovateViolationV2D(
			RenovateViolationVO renovateViolationVO,
			RenovateViolationDO renovateViolationDO) {
//		PopulateUtil.populate(renovateViolationVO, renovateViolationDO);
		BeanUtil.convertVoAsDo(renovateViolationVO, renovateViolationDO);
		if(BlankUtil.isBlank(renovateViolationVO.getReformDate())) {
			renovateViolationDO.setReformDate(null);
		}

		if(BlankUtil.isBlank(renovateViolationVO.getAmerceClient())) {
			renovateViolationDO.setAmerceClient(new BigDecimal(0));
		}

		if(BlankUtil.isBlank(renovateViolationVO.getAmerceCompany())) {
			renovateViolationDO.setAmerceCompany(new BigDecimal(0));
		}

		if(BlankUtil.isBlank(renovateViolationVO.getSeizureRes())) {
			renovateViolationDO.setSeizureRes("");
		}

		if(BlankUtil.isBlank(renovateViolationVO.getOtherRemark())) {
			renovateViolationDO.setOtherRemark("");
		}

		if (!BlankUtil.isBlank(renovateViolationVO.getRenovateLicenceId())) {
			RenovateLicenceDO renovateLicenceDO = renovateLicenceDao
					.findById(renovateViolationVO.getRenovateLicenceId());
			renovateLicenceDO.getRenovateRequisition().setBurdenManPhone(
					renovateViolationVO.getBurdenManPhone());
			renovateLicenceDO.getRenovateRequisition().setLinkManPhone(
					renovateViolationVO.getLinkManPhone());
			renovateViolationDO.setRenovateLicence(renovateLicenceDO);
		} else {
			renovateViolationDO.getRenovateLicence().getRenovateRequisition()
					.setBurdenManPhone(renovateViolationVO.getBurdenManPhone());
			renovateViolationDO.getRenovateLicence().getRenovateRequisition()
					.setLinkManPhone(renovateViolationVO.getLinkManPhone());
		}
		return renovateViolationDO;
	}

	/**
	 * @author 许心炜
	 * @param renovateViolationDO
	 * @param renovateViolationVO
	 * @return RenovateViolationVO
	 * @roseuid 493F2835034B
	 */
	private RenovateViolationVO assembleRenovateViolationD2V(
			RenovateViolationDO renovateViolationDO,
			RenovateViolationVO renovateViolationVO) {
//		PopulateUtil.populate(renovateViolationDO, renovateViolationVO, arg2);
		BeanUtil.convertVoAsDo(renovateViolationDO, renovateViolationVO);
		// 整改状态 001-未整改， 002-不通过， 003-通过
		if (renovateViolationVO.getState().equals("001")) {
			renovateViolationVO.setStateName("未整改");
		} else if (renovateViolationVO.getState().equals("002")) {
			renovateViolationVO.setStateName("不通过");
		} else if (renovateViolationVO.getState().equals("003")) {
			renovateViolationVO.setStateName("通过");
		}

		renovateViolationVO.setLinkMan(renovateViolationDO.getRenovateLicence()
				.getRenovateRequisition().getLinkman());
		renovateViolationVO.setHouseId(renovateViolationDO.getRenovateLicence()
				.getRenovateRequisition().getHouse().getKeyword());
		renovateViolationVO.setHouseNumber(renovateViolationDO
				.getRenovateLicence().getRenovateRequisition().getHouse()
				.getHouseNumber());
		renovateViolationVO.setBurdenMan(renovateViolationDO
				.getRenovateLicence().getRenovateRequisition().getBurdenMan());
		renovateViolationVO.setBurdenManPhone(renovateViolationDO
				.getRenovateLicence().getRenovateRequisition()
				.getBurdenManPhone());
		renovateViolationVO.setRenovateLicenceNO(renovateViolationDO
				.getRenovateLicence().getLicenceNumber());
		renovateViolationVO.setOwner(renovateViolationDO.getRenovateLicence()
				.getRenovateRequisition().getClientNames());
		return renovateViolationVO;
	}

	/**
	 * @author 许心炜
	 * @param reformVO
	 * @param reformDO
	 * @return com.ce.pms.renovation.model.ReformDO
	 * @roseuid 493F32B8031C
	 */
	private ReformDO assembleReformV2D(ReformVO reformVO, ReformDO reformDO) {
		RenovateViolationDO renovateViolationDO = new RenovateViolationDO();
		renovateViolationDO = (RenovateViolationDO) renovateViolationDao
				.findById(reformVO.getRenovateViolationId());
		reformDO.setRenovateViolationDO(renovateViolationDO);
		BeanUtil.convertVoAsDo(reformVO, reformDO);

		return reformDO;
	}

	/**
	 * @author 许心炜
	 * @param reformDO
	 * @param reformVO
	 * @return ReformVO
	 * @roseuid 493F330302BF
	 */
	private ReformVO assembleReformD2V(ReformDO reformDO, ReformVO reformVO) {

		BeanUtil.convertVoAsDo(reformDO, reformVO);
		// 验收结果 001-通过，002-不通过
		if ("001".equals(reformVO.getResult())) {
			reformVO.setResultName("通过");
		} else if ("002".equals(reformVO.getResult())) {
			reformVO.setResultName("不通过");
		}
		return reformVO;
	}

	/**
	 * 高级查询的D2V
	 *
	 * @author 许心炜
	 * @param renovateViolationConditionVO
	 * @param renovateViolationDO
	 * @return RenovateViolationConditionVO
	 */
	private RenovateViolationConditionVO assembleRenovateViolationConditionD2V(
			RenovateViolationDO renovateViolationDO,
			RenovateViolationConditionVO renovateViolationConditionVO) {

		renovateViolationConditionVO.setCheckMan(renovateViolationDO
				.getCheckMan());
		renovateViolationConditionVO.setHouseNumber(renovateViolationDO
				.getRenovateLicence().getRenovateRequisition().getHouse()
				.getHouseNumber());
		renovateViolationConditionVO.setRenovateLicenceNO(renovateViolationDO
				.getRenovateLicence().getLicenceNumber());
		renovateViolationConditionVO.setCheckDateStr(getDateString(
				"yyyy-MM-dd", renovateViolationDO.getCheckDate()));
		renovateViolationConditionVO.setState(renovateViolationDO.getState());

		// 整改状态 001-未整改， 002-不通过， 003-通过
		if (renovateViolationConditionVO.getState().equals("001")) {
			renovateViolationConditionVO.setStateName("未整改");
		} else if (renovateViolationConditionVO.getState().equals("002")) {
			renovateViolationConditionVO.setStateName("不通过");
		} else if (renovateViolationConditionVO.getState().equals("003")) {
			renovateViolationConditionVO.setStateName("通过");
		}

		renovateViolationConditionVO.setKeyword(renovateViolationDO
				.getKeyword());
		renovateViolationConditionVO.setDescription(renovateViolationDO
				.getDescription());

		renovateViolationConditionVO
				.setOwner(renovateViolationDO.getRenovateLicence()
						.getRenovateRequisition().getClientNames());

		return renovateViolationConditionVO;
	}

	/**
	 * 通知删除整改记录
	 *
	 * @author 许心炜
	 * @param reformId -
	 *            回访Id
	 * @return void
	 * @roseuid 493F384C02CE
	 */
	private void notifyDeleteReform(Long reformId) {
		ReformDO reformDO = reformDao.findById(reformId);

		int recordNO = reformDao.getReforms(
				reformDO.getRenovateViolationDO().getKeyword()).size();

		// 验收结果，001-通过，002-不通过;
		// 整改状态，001-未整改， 002-不通过， 003-通过
		// 如果删除的时候，删除完这条记录就把全部记录都删除了，这时必须把整改记录的状态置为001-未整改
		if (recordNO == 1) {
			reformDO.getRenovateViolationDO().setState("001");
		} else if (recordNO > 1 && "001".equals(reformDO.getResult())) {
			reformDO.getRenovateViolationDO().setState("002");
		}

	}

	/**
	 * 通知新增整改记录
	 *
	 * @author 许心炜
	 * @param reformId -
	 *            整改记录ID
	 * @return void
	 * @roseuid 493F390D02AF
	 */
	private void notifyAddReform(Long reformId) {
		ReformDO reformDO = reformDao.findById(reformId);

		// 验收结果，001-通过，002-不通过;
		// 整改状态，001-未整改， 002-不通过， 003-通过
		if ("001".equals(reformDO.getResult())) {
			reformDO.getRenovateViolationDO().setState("003");
		}

		if ("002".equals(reformDO.getResult())) {
			reformDO.getRenovateViolationDO().setState("002");
		}
	}

	/**
	 * 功能描述: 检查违规登记必填字段是否为空<br>
	 *
	 * @param renovateViolationVO
	 * @return Create author:许心炜 <br>
	 *         Create on:2009-1-6 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private Boolean checkRenovateViolationField(
			RenovateViolationVO renovateViolationVO) {
		if (BlankUtil.isBlank(renovateViolationVO.getHouseNumber())
				|| BlankUtil.isBlank(renovateViolationVO.getCheckMan())
				|| renovateViolationVO.getCheckDate() == null
				|| renovateViolationVO.getViolateDate() == null
				|| BlankUtil.isBlank(renovateViolationVO.getDescription())) {
			return false;
		}

		if (BlankUtil.isBlank(renovateViolationVO.getAmerceYn())
				&& BlankUtil.isBlank(renovateViolationVO.getOtherYn())
				&& BlankUtil.isBlank(renovateViolationVO.getSeizureYn())
				&& BlankUtil.isBlank(renovateViolationVO.getStopBehaviorYn())
				&& BlankUtil.isBlank(renovateViolationVO.getStopRenovateYn())
				&& BlankUtil.isBlank(renovateViolationVO.getTermReformYn())) {
			return false;
		}

		if (renovateViolationVO.getCheckDate().before(
				renovateViolationVO.getViolateDate())) {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_checkdate_before_violationdate",
					BusinessException.ERROR);
		}

		return true;
	}

	/**
	 * 功能描述: 检查整改记录的必填字段是否为空 <br>
	 *
	 * @param reformVO
	 * @return Create author:许心炜 <br>
	 *         Create on:2009-1-6 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private Boolean checkReformField(ReformVO reformVO) {

		if ("002".equals(reformVO.getResult())) {// 验收结果 001-通过，002-不通过
			if (BlankUtil.isBlank(reformVO.getResult())
					|| BlankUtil.isBlank(reformVO.getCheckMan())
					|| reformVO.getCheckDate() == null
					|| BlankUtil.isBlank(reformVO.getRemark())) {
				return false;
			}
		} else if ("001".equals(reformVO.getResult())) {
			if (BlankUtil.isBlank(reformVO.getResult())
					|| BlankUtil.isBlank(reformVO.getCheckMan())
					|| reformVO.getCheckDate() == null) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 功能描述: 获得指定格式的时间字符串，如果参数有一个为空，将返回null <br>
	 *
	 * @param pattern
	 * @param date
	 * @return Create author:许心炜 <br>
	 *         Create on:2008-12-29 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private String getDateString(String pattern, Date date) {
		if (pattern == null || date == null) {
			return null;
		} else {
			SimpleDateFormat sf = new SimpleDateFormat(pattern);
			return sf.format(date);
		}
	}

	public void setRenovateViolationDao(
			IRenovateViolationDao renovateViolationDao) {
		this.renovateViolationDao = renovateViolationDao;
	}

	public void setReformDao(IReformDao reformDao) {
		this.reformDao = reformDao;
	}

	public void setRenovateLicenceDao(IRenovateLicenceDao renovateLicenceDao) {
		this.renovateLicenceDao = renovateLicenceDao;
	}

	public void setRenovateManageService(
			IRenovateManageService renovateManageService) {
		this.renovateManageService = renovateManageService;
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

	public void setInspectionRequisitionDao(
			IInspectionRequisitionDao inspectionRequisitionDao) {
		this.inspectionRequisitionDao = inspectionRequisitionDao;
	}

}
